package com.dollar.juc.lock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 实现自旋锁
 */
public class SpinLock {
    AtomicReference<Thread> owner = new AtomicReference<>();

    public void lock(){
        Thread cur = Thread.currentThread();
        while(!owner.compareAndSet(null, cur)){

        }
    }

    public void unlock(){
        Thread cur = Thread.currentThread();
        if(cur.equals(owner.get())){
            owner.compareAndSet(cur, null);
        }
    }

}
